from django.http import HttpResponse
from django.template import Context
from django.template.loader import get_template
from django.http import HttpResponse, Http404
from django.contrib.auth.models import User
from django.template import RequestContext  
from django.http import HttpResponseRedirect
from django.contrib.auth import logout
from django.shortcuts import render_to_response
from forfood.restaurant.models import *
from forfood.menu.models import *
from forfood.order.models import *

def menu_page(request):
    if  request.method == 'GET':
        if 'rest_id' in request.GET and 'menu_item_id' not in request.GET and 'menu_item_num' not in request.GET:
            rest_id = request.GET['rest_id']
            rest_p = Restaurant.objects.get(id=rest_id)
            menu_item_all_list = display_menu_item(rest_id)
                   
            return render_to_response(
                    'menu/menu_list.html', {'menu_item_all_list':menu_item_all_list, 'rest_p':rest_p}
                   )
        elif 'rest_id' in request.GET and 'menu_item_id' in request.GET and 'menu_item_num' in request.GET:
            customer_id_list = []
            user_list = User.objects.all()
            customer_count = customer_list.count()
            i = 0
            if customer_count > 0:
                print("1")
                while i < customer_count:
                    customer_id_list = customer_id_list + [user_list[i].id]
                    i = i + 1
                print("2")
                customer_id_one = customer_id_list[0]
                print("sd%d" %customer_id_one)
                customer_p = User.objects.get(id=customer_id_one)
                print("4")
                order_list = customer_p.order_set.all()
                print("5")
                if order_list:
                    pass
                else:
                    print("6")
                    rest_p = Restaurant.objects.get(id=request.GET['rest_id'])
                    item_p = MenuItem.objects.get(id=request.GET['menu_item_id'])
                    price = item_p.price
                    item_num = request.GET['menu_item_num']
                    total = price*item_num
                    order = Order(customer=coustomer_p ,restaurant=rest_p, item=item_p,total=total)
                    customer_p = Customer.objects.get(user_ptr_id=customer_id_list[0])
                    order_list = customer_p.order_set.all()
                    order = order_list[0]
                    item_list = order.item.all()
                    
                    return render_to_response(
                        'menu/menu_list.html', {'menu_item_all_list':menu_item_all_list, 'rest_p':rest_p,
                                                'order_list':order_list, 'item_list':item_list}
                        )
                
            else:                
                return HttpResponse('The user dose not exist!')
    
    
    
    
 #   elif 'rest_id' in request.GET and 'menu_item_num' in request.GET and 'menu_item_id' in request.GET:
 #       menu_item_id = request.GET['menu_item_id']
 #       menu_item_num = request.GET['menu_item_num']
 #       return HttpResponse('The restaurant does not exist!')
        
 #   else:
 #       return HttpResponse('The restaurant does not exist!')

def display_menu_item(rest_id):
    rest_p = Restaurant.objects.get(id=rest_id)
    menu_list = rest_p.menu_set.all()
    menu_count = menu_list.count()   
    i = 0
    menu_id_list = []

    while i < menu_count:
        menu_id_list = menu_id_list + [menu_list[i].id]
        i = i + 1

    menu_category_id_list = []
    
    for j in menu_id_list:
        i = 0
        menu_p = Menu.objects.get(id=j)
        menu_category_list = menu_p.menucategory_set.all()
        menu_category_count = menu_category_list.count()
        while i < menu_category_count:
            menu_category_id_list = menu_category_id_list + [menu_category_list[i].id]
            i = i + 1

    menu_item_id_list = []
    
    for j in menu_category_id_list:
        i = 0
        menu_item_p = MenuCategory.objects.get(id=j)
        menu_item_list = menu_item_p.menuitem_set.all()
        menu_item_count = menu_item_list.count()
        while i < menu_item_count:
            menu_item_id_list = menu_item_id_list + [menu_item_list[i].id]
            i = i + 1

    menu_item_all_list = MenuItem.objects.filter(pk__in=menu_item_id_list)

    return  menu_item_all_list